		TITLE	ALLOVTBL - Copyright (c) SLR Systems 1991

		INCLUDE MACROS

if	any_overlays

		PUBLIC	ALLOCATE_VECTOR_TABLE

		.DATA

	SOFT	EXTW	VECTOR_COUNT,VECTOR_SIZE,PUBALLOC_STUFF

	SOFT	EXTD	FIRST_MODULE

		.CODE	MIDDLE_TEXT

	SOFT	EXTP	RETURN_LOWEST_SECTION,ERR_ABORT,RELEASE_MINIDATA

	SOFT	EXTA	TOO_MANY_VECTORS_ERR

		ASSUME	DS:NOTHING

ALLOCATE_VECTOR_TABLE	PROC
		;
		;ALLOCATE SPACE FOR THUNKS.
		;
		;
		;FIRST, PROCESS PUBALLOC TABLE TO HANDLE ANY ALLOCATABLE
		;REFERENCES
		;
		CALL	PROCESS_PUBALLOCS
		;
		;OK, SCAN SYMBOL TABLE FOR SYMBOLS THAT NEED TO BE VECTORED
		;EITHER BECAUSE:
		;	1. MARKED VECTOR ALWAYS
		;	2. MODULE WAS MARKED VECTOR ALWAYS (HANDLE AT PUBDEF)
		;	3. NOT MARKED NEVER, AND WAS REFERENCED BY A LOWER
		;		SECTION
		;
		;MARK SYMBOL WITH A VECTOR #
		;
		LDS	SI,FIRST_MODULE
		JMP	81$

2$:
		SYM_CONV_DS
		PUSHM	[SI]._S_NEXT_SYM.OFFS,[SI]._S_NEXT_SYM.SEGM
		;
		;ONLY INTERESTED IN RELOC OR LOC_RELOC
		;
		MOV	AL,[SI]._S_NSYM_TYPE
;		CMP	AL,NSYM_LOC_RELOCATABLE			;for wordtech
;		JZ	21$
		CMP	AL,NSYM_RELOCATABLE
		JNZ	7$
21$:
		;
		;
		;
		CMP	[SI]._S_SECTION.SEGM,0	;IGNORE IF NEVER REFERENCED
		JZ	4$
		MOV	AL,[SI]._S_PLINK_FLAGS
		TEST	AL,MASK VECTOR_ALWAYS
		JNZ	31$
		TEST	AL,MASK VECTOR_NEVER
		JNZ	4$
		;
		;VECTOR MAYBE
		;
		LES	DI,[SI]._S_FRAME
		MOV	DX,ES
		SYM_CONV_ES
		TEST	ES:[DI]._SM_PLTYPE,MASK LEVEL_0_SECTION
		JNZ	4$
		BITT	ALWAYSALL_FLAG
		JNZ	30$			;VECTOR ALL
		PUSHM	SI,CX,DI,DX

		LEA	SI,[SI]._S_SECTION
		LEA	DI,[DI]._SM_SECTION
		CALL	RETURN_LOWEST_SECTION
		POPM	ES,DI,DS,SI
		SYM_CONV_ES
		CMP	ES:[DI]._SM_SECTION.OFFS,BX
		JNZ	3$
		CMP	ES:[DI]._SM_SECTION.SEGM,DX
		JZ	4$
3$:
		SYM_CONV_DS
		JMP	31$

30$:
		OR	[SI]._S_PLINK_FLAGS,MASK VECTOR_ALWAYS
31$:
		OR	[SI]._S_PLINK_FLAGS,MASK VECTOR_YES
;               MOV     AX,VECTOR_COUNT
;		MOV	[SI]._S_VECTOR,AX
;		INC	AX
		INC	VECTOR_COUNT
		JZ	99$
;               MOV     VECTOR_COUNT,AX
4$:
7$:
		POPM	DS,SI
71$:
		MOV	CX,DS
		OR	CX,CX
		JNZ	2$
8$:
		POPM	DS,SI
81$:
		MOV	AX,DS
		OR	AX,AX
		JNZ	1$
		MOV	DX,1
		DIV	VECTOR_SIZE		;PROBABLY 8 OR 12
		CMP	VECTOR_COUNT,AX
		JA	99$
		RET

1$:
		SYM_CONV_DS
		PUSHM	[SI]._M_NEXT_MOD.OFFS,[SI]._M_NEXT_MOD.SEGM
		;
		;SCAN MY PUBDEFS FOR POSSIBLE VECTORS
		;
		LDS	SI,[SI]._M_FIRST_PUB
		JMP	71$

99$:
		MOV	CL,TOO_MANY_VECTORS_ERR
		JMP	ERR_ABORT

ALLOCATE_VECTOR_TABLE	ENDP

PROCESS_PUBALLOCS	PROC	NEAR
		;
		;HANDLE ANY REFERENCES FROM ALLOCATABLE SEGMODS
		;
		LDS	SI,PUBALLOC_STUFF.ALLO_FIRST_ITEM
		MOV	CX,DS
		JCXZ	9$
1$:
		SYM_CONV_DS
		PUSHM	[SI]._PUBALLOC_NEXT.OFFS,[SI]._PUBALLOC_NEXT.SEGM
		;
		;IF SYMBOL IS NOT A NORMAL RELOCATABLE SYMBOL, IGNORE
		;
		LES	DI,[SI]._PUBALLOC_FROM
		LDS	SI,[SI]._PUBALLOC_TO
		MOV	CX,DS
		SYM_CONV_DS
		MOV	AL,[SI]._S_NSYM_TYPE
		CMP	AL,NSYM_LOC_RELOCATABLE
		JZ	2$
		CMP	AL,NSYM_RELOCATABLE
		JNZ	38$
2$:
		;
		;IF SYMBOL IS LEVEL_0_SECTION, IGNORE
		;
		TEST	[SI]._S_PLTYPE,MASK LEVEL_0_SECTION
		JNZ	38$
		;
		;MAKE SURE SYMBOL'S SECTION REFLECTS LOWEST SYMBOL REFERENCE
		;
		SYM_CONV_ES
		PUSHM	CX,SI
		LEA	DI,[DI]._SM_SECTION
		LEA	SI,[SI]._S_SECTION
		CALL	RETURN_LOWEST_SECTION
		POPM	SI,CX
		MOV	DS,CX
		SYM_CONV_DS
		CMP	[SI]._S_SECTION.OFFS,BX
		JZ	35$		;PROBABLY A MATCH
		MOV	[SI]._S_SECTION.OFFS,BX
36$:
		MOV	[SI]._S_SECTION.SEGM,DX
		MOV	ES,DX
		SYM_CONV_ES
		CMP	ES:[BX]._SECT_LEVEL,0
		JNZ	37$
		OR	[SI]._S_PLTYPE,MASK LEVEL_0_SECTION
		;
		;YOU ARE DONE IF ALREADY LEVEL_0_SECTION
		;
		JMP	37$

35$:
		CMP	[SI]._SM_SECTION.SEGM,DX
		JNZ	36$
37$:
38$:
		POPM	DS,SI
		MOV	CX,DS
		OR	CX,CX
		JNZ	1$
		LEA	DI,PUBALLOC_STUFF
		CALL	RELEASE_MINIDATA
9$:
		RET

PROCESS_PUBALLOCS	ENDP

endif

		END

